<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input v-model="employeeForm.keyword" style="margin-bottom:10px" type="text" prefix-icon="el-icon-search" size="small" placeholder="输入员工姓名全员搜索" @input="handleInput" />
        <!-- 树形组件 -->
        <el-tree
          ref="tree"
          :data="departmentList"
          :props="defaultProps"
          node-key="id"
          check-strictly
          default-expand-all
          highlight-current
          @node-click="handleNodeClick"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
          <el-button size="mini" @click="isShow = true">excel导入</el-button>
          <!-- v-if="$store.state.user.userInfo.roles.points.includes('excel_export')" -->
          <el-button
            v-allow="'excel_export'"
            size="mini"
            @click="exportEmployee"
          >excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="employeeList">
          <el-table-column align="center" label="头像">
            <template v-slot="{ row }">
              <img v-if="row.staffPhoto" :src="row.staffPhoto" alt="" style="width: 30px">
              <span v-else class="username">{{ row.username.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column label="聘用形式">
            <template v-slot="{ row }">
              <span>{{ row.formOfEmployment ? '正式' : '非正式' }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作" width="280px">
            <template #default="{ row }">
              <el-button size="mini" type="text" @click="$router.push('/employee/detail/' + row.id)">查看</el-button>
              <el-button size="mini" type="text" @click="openRoleDialog(row.id)">角色</el-button>
              <el-button size="mini" type="text">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          :current-page="employeeForm.page"
          :background="true"
          :page-size="employeeForm.pagesize"
          layout="prev, pager, next, sizes"
          :total="total"
          :page-sizes="[5, 6, 10, 8, 100]"
        />
      </div>
    </div>
    <import-excel :is-show.sync="isShow" @updateLoad="getDepartmentList" />
    <assign-role ref="RoleDialog" :current-id="currentId" :show-role-dialog.sync="showRoleDialog" />
  </div>
</template>

<script>
import { newArr } from '@/utils'
import { getDepartment } from '@/api/department'
import { getEmployeeList, getExportEmployeeList } from '@/api/employee'
import FileSaver from 'file-saver'
import ImportExcel from './components/import-excel.vue'
import AssignRole from './components/assignRole.vue'
export default {
  name: 'Employee',
  components: {
    ImportExcel, AssignRole
  },
  data() {
    return {
      currentId: -1,
      defaultProps: { label: 'name', children: 'children' },
      departmentList: [
        { name: '传智教育', children: [{ name: '总裁办' }] }
      ],
      employeeForm: {
        page: 1,
        pagesize: 10,
        keyword: '',
        departmentId: ''
      },
      total: 0,
      employeeList: [],
      isShow: false,
      showRoleDialog: false
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    async getDepartmentList() {
      const res = await getDepartment()
      console.log(res)
      const departmentRes = newArr(res.data, 0)
      this.departmentList = departmentRes
      const id = departmentRes[0].id
      this.employeeForm.departmentId = id
      this.$nextTick(() => {
        // this.$refs.tree.setCheckedKeys([id])
        this.$refs.tree.setCurrentKey(id)
      })
      this.getEmployee()
    },
    handleNodeClick(node) {
      console.log(node)
    },
    async getEmployee() {
      const res = await getEmployeeList(this.employeeForm)
      console.log(res)
      this.total = res.data.total
      this.employeeList = res.data.rows
    },
    handleInput() {
      clearTimeout(this.timer)
      // 防抖 单位时间内,多次触发事件,只会执行一次
      this.timer = setTimeout(() => {
        this.employeeList.page = 1
        this.getEmployee()
      }, 500)
    },
    async exportEmployee() {
      const res = await getExportEmployeeList()
      console.log(res)
      FileSaver.saveAs(res, '员工信息表.xlsx')
    },
    openRoleDialog(id) {
      // 保存id
      console.log(id)
      this.currentId = id
      this.showRoleDialog = true
      // 调用子组件的方法,获取到当前员工的详情,因为它是个异步的请求
      // 所以要用$nextTick()包裹
      this.$nextTick(() => {
        this.$refs.RoleDialog.getRoleById()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>
